import config from '../common/config.js'
import Cache from "../utils/cache"; 
import utils from "../utils/index.js"; 
// let isShowModal = false
const apiResquest = (prams) => { //prams 为我们需要调用的接口API的参数 下面会贴具体代码  
    let headerData={}
	let token = Cache.get('access_token'); 
	let dataObj = null
	//因为我们的GET和POST请求结构不同这里我们做处理，大家根据自己后台接口所需结构灵活做调整吧
	if (prams.method === "GET") {
		headerData = { }
		dataObj = prams.query
	} else {
		dataObj = prams.query
		headerData = {
			'Content-Type': 'application/json'
		}

	}
	headerData['Authorization'] = 'Bearer ' + token

	return new Promise((resolve, reject) => { 
		
		let url = prams.url.startsWith('/')
		  ? config.http.baseURL + prams.url
		  : config.http.baseURL + '/' + prams.url;//请求的网络地址和局地的api地址组合 
		  
		return uni.request({
			url: url,
			data: dataObj,
			method: prams.method,
			header: headerData,
			timeout: config.http.timeout,
			success: (response) => { 
				let rs = {
					...response
				} 
				if ((rs.data && rs.data.code == 500)) {
					if(!rs.data.msg){
						uni.showToast({
							icon: "none",
							title: utils.isAllChinese(rs.data.msg) ? rs.data.msg : "网络错误，请稍后再试"
						});
						return;
					}
					if(rs.data.msg.indexOf('oauth_code')!= -1){
						window.location.reload()
						return;
					} 
					uni.showToast({
						icon: "none",
						title: utils.isAllChinese(rs.data.msg) ? rs.data.msg : "网络错误，请稍后再试"
					});
					reject( utils.isAllChinese(rs.data.msg) ? new Error(rs.data.msg) : new Error('网络错误，请稍后再试'));
				}else if(rs.data && rs.data.code == 401 ){ 
					if(url.includes('app/area')&& !token){
						return false
					}
					uni.clearStorageSync()
					uni.reLaunch({
						url: '/pages/mine/modules/userLogin/userLogin'
					})
					uni.showToast({
						icon: "none",
						title:  "登录已过期，请重新登录"
					});
					reject(new Error('登录已过期，请重新登录'));
				} else {
					resolve(rs.data);
				}
				
			},
			fail: (err) => {
				console.log(err)
				reject(err); 
			},
			complete: () => { 
			}
		});
	})
}
const http = {
	get(t, n) {
		return apiResquest({
			query: n,
			method: 'GET',
			url: t
		})
	},
	post(t, n) {
		return apiResquest({
			query: n,
			method: 'POST',
			url: t
		})
	},
	delete(t, n) {
		return apiResquest({
			query: n,
			method: 'DELETE',
			url: t
		})
	},
	put(t, n) {
		return apiResquest({
			query: n,
			method: 'PUT',
			url: t
		})
	},
	getList(t, n) {
		return apiResquest({
			query: n,
			method: 'GET',
			url: t + '/list'
		})
	},
	all(...t) {
		return new Promise((success, fail) => {

		})
	}
};

export const axios = (url) => {
	const request = {
		...http
	}
	request.get = (params) => {
		return apiResquest({
			query: params,
			method: 'GET',
			url
		})
	}
	request.post = (params) => {
		return apiResquest({
			query: params,
			method: 'POST',
			url
		})
	}
	request.put = (params) => {
		return apiResquest({
			query: params,
			method: 'PUT',
			url
		})
	}
	request.delete = (params) => {
		return apiResquest({
			query: params,
			method: 'DELETE',
			url
		})
	}
	request.getList = (params) => {
		return apiResquest({
			query: params,
			method: 'GET',
			url: url + '/list'
		})
	}

	request.fetch = (queryUrl, params, method) => {
		return apiResquest({
			query: params,
			method,
			url: url + '/' + queryUrl
		})
	}


	return request
}
export default http
